package servlet;

import jopo.ValidateCode;

import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet(value = "/getCode",loadOnStartup = -1)

public class ValidateCodeServlet extends HttpServlet{
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setContentType("image/jpeg");
        response.setHeader("Pragma", "no-cache");
        response.setHeader("Cache-Control", "no-cache");
        response.setDateHeader("Expires", 0);
        ValidateCode validateCode = new ValidateCode();
        request.getSession().setAttribute("code",validateCode.getCode());
        ImageIO.write(validateCode.getBuffImg(), "JPG", response.getOutputStream());
    }
    //Implementations supporting the GET request should override this method to provide an accurate object modification time.
    // This makes browser and proxy caches work more effectively, reducing the load on server and network resources.
    @Override
    protected long getLastModified(HttpServletRequest req){
        return System.currentTimeMillis();
    }
}
